package com.ultisense.gassensor;

import android.util.Log;

import java.util.Stack;

/**
 * Created by zchenak on 1/12/2018.
 */

class DataProcessor {
    private static float[] allData;
    public static long sensorTime;
    public static long sensorFlagTime;
    //    private static float[] saveData;
    final static int Rair = 1024;
    static int dataAvrCount;

    public DataProcessor() {
        allData = new float[16];
//        saveData = new float[10];
        dataAvrCount = 0;
    }

    public static void storeData(byte[] data) {
        int position = -1;
        String processedData = "";
        int mark = 2;
        String rawdata=bytesToHexString(data);
        String data_d=bytesToDecString(data);
        Log.i("rawdata",rawdata);
        Log.i("10data",data_d);
        for (int sensorNum = 0; sensorNum < 16; sensorNum++) {

            //BLE data package: 0x42 0x01 (16 nodes data on 32 bytes) check
            if (data.length != 35) {
                sensorTime = getSensorTime(data);
                Log.e("data check", "dataT");
                sensorFlagTime = getSensorFlagTime(data);
            } else {
                Log.e("data check", "dataNT");
            }
            dataCheck(data);
            //total 35
//            Nodenum=byteToInt(data[2]);
            float a = byteToInt(data[2 * sensorNum + 2]);
            float b = byteToInt(data[2 * sensorNum + 3]);
            float sensorValue = a * 256 + b;//ADC度数电阻值

//            float mathData = (1024 - sensorValue);
            float mathData = sensorValue;

            allData[sensorNum] = mathData;
        }
    }

    public static int byteToInt(byte b) {
        //Java 总是把 byte 当做有符处理；我们可以通过将其和 0xFF 进行二进制与得到它的无符值
        return b & 0xFF;
    }

    public static float valueProcess(int sensorValue) {
        float processedValue = sensorValue;
        return processedValue;
    }

    public static long getSensorTime(byte[] data) {
        long time = 0;
        if (data.length > 38) {
            time = byteToInt(data[35]) + (256 * byteToInt(data[36])) + (256 * 256 * byteToInt(data[37])) + (256 * 256 * 256 * byteToInt(data[38]));
        }
        return time;
    }

    public static long getSensorFlagTime(byte[] data) {
        long time = 0;
        if (data.length > 42) {
            time = byteToInt(data[39]) + (256 * byteToInt(data[40])) + (256 * 256 * byteToInt(data[41])) + (256 * 256 * 256 * byteToInt(data[42]));
        }
        return time;
    }

    public static float getSingleData(int sensor) {
        return allData[sensor];
    }

    public static double sensitivity(double base, double num) {
        double sensibility = 0;
        sensibility = Math.abs(num - base) / base * 1;
        return sensibility;
    }

    public static double concentration(double sensitivity, int type) {
        double concentration = 0;
        //type 1 h2 2 no2 3 fa 4 benz
        switch (type) {
            case 1://y = 4532.7x2 + 25.713x + 8.5726（if sensi > 0.15）
                if (sensitivity <= 0.047) {
                    concentration = 0.859 * Math.exp(sensitivity*124.069) + 1.8;
                } else {//y = 20361x2 - 10444x + 1693.7
                    concentration = 2316.88 * Math.exp(sensitivity*2.59) - 2045.15;
                }

//                concentration=12274.93*Math.pow(sensitivity,2.948);
                break;
            case 2:
//                if (sensitivity < 0.018) {
//                    concentration = 69.786;
//                } else if (sensitivity < 0.107) {//y =  43413x2 + 4036.8x + 69.786
//                    concentration = (43413 * sensitivity * sensitivity) + (4036.8 * sensitivity) + 69.786;
//                } else {//y = 25342x2 + 21978x - 1774.5
//                    concentration = (25342 * sensitivity * sensitivity) + (21978 * sensitivity) - 1774.5;
//                }
                concentration=0;
//                concentration=46017.54*Math.pow(sensitivity,1.571);
                break;
            case 3:
//                if (sensitivity < 0.0019) {
//                    concentration = 30.761;
//                } else if (sensitivity < 0.0218) {//y = -118729x2 + 10352x + 30.761
//                    concentration = (-118729 * sensitivity * sensitivity) + (10352 * sensitivity) + 30.761;
//                } else {//y = 343634x2 - 9538x + 247.91
//                    concentration = (343634 * sensitivity * sensitivity) - (9538 * sensitivity) + 247.91;
//                }

                concentration=0;
//                concentration=108800.22*Math.pow(sensitivity,1.7046);
                break;
            case 4:
//                if (sensitivity < 0.0014) {
//                    concentration = 38.561;
//                } else if (sensitivity < 0.0182) {//y = 41651x2 + 8112.2x + 38.561
//                    concentration = (41651 * sensitivity * sensitivity) + (8112.2 * sensitivity) + 38.561;
//                } else {//y = 645527x2 - 20635x + 366.9
//                    concentration = (645527 * sensitivity * sensitivity) - (20635 * sensitivity) + 366.9;
//                }
                concentration=0;
//                concentration=211182.56*Math.pow(sensitivity,1.814);
                break;
        }
        return concentration;
    }

    public static void dataCheck(byte[] data) {
        byte checksum = 0;
        byte sum = 0;
        for (int i = 2; i < 34; i++) {
            sum = (byte) (sum + data[i]);
        }
        checksum = (byte) (sum ^ 0xFF);
        if (checksum != data[34]) {
            Log.e("datacheck", "wrong data checknum");
        }
    }


    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);

            stringBuilder.append(i + ":");

            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv + ";");
        }
        return stringBuilder.toString();
    }

    public static String bytesToDecString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i]&0xFF;
            String hv = Integer.toString(v);

            stringBuilder.append(i + ":");

            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv + ";");
        }
        return stringBuilder.toString();
    }

//    public static int charArrayToInt(char[] data, int start, int end) throws NumberFormatException {
//        int result = 0;
//        for (int i = start; i < end; i++) {
//            int digit = (int) data[i] - (int) '0';
//            if ((digit < 0) || (digit > 9)) throw new NumberFormatException();
//            result *= 10;
//            result += digit;
//        }
//        return result;
//    }
//
//    public static String charArrayToString(char[] data, int start, int end) {
//        StringBuilder stringBuilder = new StringBuilder();
//        for (int i = start; i < end; i++) {
//            stringBuilder.append(data[i]);
//        }
//        return stringBuilder.toString();
//    }
}
